package com.ruoyi.zhgd.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.zhgd.domain.bo.ZjFurnaceRequiresBo;
import com.ruoyi.zhgd.domain.vo.ZjFurnaceRequiresVo;
import com.ruoyi.zhgd.domain.ZjFurnaceRequires;
import com.ruoyi.zhgd.mapper.ZjFurnaceRequiresMapper;
import com.ruoyi.zhgd.service.IZjFurnaceRequiresService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 燃烧炉技术要求Service业务层处理
 *
 * @author zyb
 * @date 2023-10-13
 */
@RequiredArgsConstructor
@Service
@DS("sjcj")
public class ZjFurnaceRequiresServiceImpl implements IZjFurnaceRequiresService {

    private final ZjFurnaceRequiresMapper baseMapper;

    /**
     * 查询燃烧炉技术要求
     */
    @Override
    public ZjFurnaceRequiresVo queryById(String id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询燃烧炉技术要求列表
     */
    @Override
    public TableDataInfo<ZjFurnaceRequiresVo> queryPageList(ZjFurnaceRequiresBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<ZjFurnaceRequires> lqw = buildQueryWrapper(bo);
        Page<ZjFurnaceRequiresVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询燃烧炉技术要求列表
     */
    @Override
    public List<ZjFurnaceRequiresVo> queryList(ZjFurnaceRequiresBo bo) {
        LambdaQueryWrapper<ZjFurnaceRequires> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<ZjFurnaceRequires> buildQueryWrapper(ZjFurnaceRequiresBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ZjFurnaceRequires> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getName()), ZjFurnaceRequires::getName, bo.getName());
        lqw.eq(StringUtils.isNotBlank(bo.getInsertBy()), ZjFurnaceRequires::getInsertBy, bo.getInsertBy());
        lqw.ge(bo.getInsertTime() != null, ZjFurnaceRequires::getInsertTime, bo.getInsertTime());
        lqw.eq(StringUtils.isNotBlank(bo.getDeleted()), ZjFurnaceRequires::getDeleted, bo.getDeleted());
        lqw.eq(bo.getOriginalWeight() != null, ZjFurnaceRequires::getOriginalWeight, bo.getOriginalWeight());
        lqw.eq(bo.getLostWeight() != null, ZjFurnaceRequires::getLostWeight, bo.getLostWeight());
        lqw.eq(StringUtils.isNotBlank(bo.getCorrectionCoefficient()), ZjFurnaceRequires::getCorrectionCoefficient, bo.getCorrectionCoefficient());
        lqw.eq(bo.getAsphaltContent() != null, ZjFurnaceRequires::getAsphaltContent, bo.getAsphaltContent());
        lqw.eq(bo.getAsphaltAggregateRatio() != null, ZjFurnaceRequires::getAsphaltAggregateRatio, bo.getAsphaltAggregateRatio());
        lqw.eq(bo.getMainTemperature() != null, ZjFurnaceRequires::getMainTemperature, bo.getMainTemperature());
        return lqw;
    }

    /**
     * 新增燃烧炉技术要求
     */
    @Override
    public Boolean insertByBo(ZjFurnaceRequires bo) {
        bo.setInsertTime(DateUtils.getNowDate());
        ZjFurnaceRequires add = BeanUtil.toBean(bo, ZjFurnaceRequires.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改燃烧炉技术要求
     */
    @Override
    public Boolean updateByBo(ZjFurnaceRequires bo) {
        bo.setUpdateTime(DateUtils.getNowDate());
        ZjFurnaceRequires update = BeanUtil.toBean(bo, ZjFurnaceRequires.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(ZjFurnaceRequires entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除燃烧炉技术要求
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<String> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
